import 'package:flutter/cupertino.dart';
import 'package:flutter/services.dart';
import 'package:kq_flutter_widgets/theme/kq_theme_manager.dart';

/// 扫码调用原生工具类
class ScanNativeUtil {
  static const _methodChannelName = 'flutter.keqiang.com/scan';

  static const _platformMethod = MethodChannel(_methodChannelName);

  ScanNativeUtil._inner();

  static List<int>? _mapScanType(
    List<ScanType>? scanTypes,
  ) {
    List<int>? types;
    if (scanTypes != null && scanTypes.isNotEmpty) {
      types = [];
      for (var value in scanTypes) {
        types.add(value.code);
      }
    }
    return types;
  }

  /// 扫码
  ///
  /// [title] 标题
  ///
  /// [hint] 提示文字
  ///
  /// [useAlbum] 是否开启相册选图
  ///
  /// [scanTypes] 支持的扫码类型，不传则支持所有格式
  ///
  /// [imgScanFailedBack] 扫描图片失败，是否退出扫码界面回调失败结果
  static void scanWithCallback(
      {String? title,
      String? hint,
      bool useAlbum = false,
      bool imgScanFailedBack = false,
      String? imgScanFailedHint,
      List<ScanType>? scanTypes,
      bool turnOnLight = false,
      bool showLightButton = false,
      required Function(ScanResult result) callback}) {
    scan(
      title: title,
      hint: hint,
      useAlbum: useAlbum,
      scanTypes: scanTypes,
      turnOnLight: turnOnLight,
      showLightButton: showLightButton,
    ).then((value) => callback(value));
  }

  /// 扫码
  ///
  /// [title] 标题
  ///
  /// [hint] 提示文字
  ///
  /// [useAlbum] 是否开启相册选图
  ///
  /// [scanTypes] 支持的扫码类型，不传则支持所有格式
  ///
  /// [imgScanFailedBack] 扫描图片失败，是否退出扫码界面回调失败结果
  ///
  /// [turnOnLight] 是否打开闪光灯
  ///
  /// [showLightButton] 是否展示控制闪光灯按钮
  static Future<ScanResult> scan({
    String? title,
    String? hint,
    bool useAlbum = false,
    bool imgScanFailedBack = false,
    String? imgScanFailedHint,
    List<ScanType>? scanTypes,
    bool turnOnLight = false,
    bool showLightButton = false,
  }) async {
    try {
      var result = await _platformMethod.invokeMethod('startScan', {
        'title': title,
        'hint': hint,
        'useAlbum': useAlbum,
        'imgScanFailedBack': imgScanFailedBack,
        'imgScanFailedHint': imgScanFailedHint,
        "scanTypes": _mapScanType(scanTypes),
        "navColor": KqThemeManager.getCommonConfig().mainColor?.value,
        'turnOnLight': turnOnLight,
        'showLightButton': showLightButton,
      });
      int? code = result['code'];
      return ScanResult(
          isCancel: code != 0 && code != 1,
          content: code == 1 ? result['data'] ?? "" : null,
          errorMsg: code == 0 ? result['msg'] ?? "" : null);
    } on PlatformException catch (e) {
      debugPrint(e.message);
      return ScanResult(errorMsg: e.message);
    } catch (e) {
      debugPrint(e.toString());
      return ScanResult(errorMsg: e.toString());
    }
  }
}

/// 支持的扫码格式
enum ScanType {
  ///二维码
  qrCode(0),

  /// 一维码
  bar(1);

  final int code;

  const ScanType(this.code);
}

/// 扫码结果
class ScanResult {
  const ScanResult({this.content, this.errorMsg, this.isCancel = false});

  /// 扫码内容,此值为null则表示扫码失败或扫码被取消
  final String? content;

  /// 扫码异常信息，此值不为null时表示扫码失败
  final String? errorMsg;

  /// 用户是否取消扫码
  final bool isCancel;
}
